package main;

import main.SQL.MsSQLConnect;
import main.Trees.SmetNode;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by s_shmakov on 23.05.14.
 */
public class SearchSmet {
    private ArrayList<Integer> listSmetSearch;
    private ArrayList<SmetNode> listSmets, smc, smt, smo, sms;
    private Connection connSql;
    private Statement stat;
    private HashMap<Integer, Integer> hashSmetSearch, hashSmet, listOpenSmet, sectNeed, smetNeed, objectNeed, titulNeed, complexNeed;
    private SmetNode root;
    private JDialog smetDialog;
    private JTree smetTree;


    public SearchSmet(ArrayList<Integer> listSmetSearch, HashMap<Integer, Integer> hashSmetSearch, HashMap<Integer, Integer> listOpenSmet) {
        this.listSmetSearch = listSmetSearch;
        this.hashSmetSearch = hashSmetSearch;
        this.listOpenSmet = listOpenSmet;
        this.connSql = MsSQLConnect.ConnectDB("TST");
    }

    public SmetNode search() {

        getSmetRoot();

        return root;
    }

    private ArrayList<SmetNode> getSmetRoot() {

        this.root = new SmetNode ("Найденные сметы");
        this.connSql = MsSQLConnect.ConnectDB("TST");

        getSmrList();
        getSmSectList();
        getSmsList();
        if (sms.size() > 0) {
            getSmoList();
            getSmtList();
            getSmcList();

            addHierarchy();
        } else {
            return null;
        }
        return listSmets;
    }

    private void getSmcList () {
        smc = new ArrayList<> ();

        try {
            stat = connSql.createStatement();
            ResultSet rs = stat.executeQuery("SELECT complex_id, name FROM ks_Complex");

            while (rs.next()) {
                if (complexNeed.get(rs.getInt("complex_id")) != null) {
                    smc.add(new SmetNode(0, rs.getInt("complex_id"), rs.getString("name")));
                }
            }
            rs.close();
            stat.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void getSmtList () {
        smt = new ArrayList<> ();
        complexNeed = new HashMap<>();

        try {
            stat = connSql.createStatement();
            ResultSet rs = stat.executeQuery("SELECT * FROM ks_Tituls");

            while (rs.next()) {
                if (titulNeed.get(rs.getInt("titul_id")) != null) {
                    smt.add(new SmetNode(rs.getInt("complex_id"), rs.getInt("titul_id"), rs.getString("name")));
                    complexNeed.put(rs.getInt("complex_id"), rs.getInt("complex_id"));
                }
            }

            rs.close();
            stat.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void getSmoList () {
        smo = new ArrayList<> ();
        titulNeed = new HashMap<>();

        try {
            stat = connSql.createStatement();
            ResultSet rs = stat.executeQuery("SELECT * FROM ks_Objects");

            while (rs.next()) {
                if (objectNeed.get(rs.getInt("object_id")) != null) {
                    smo.add(new SmetNode(rs.getInt("titul_id"), rs.getInt("object_id"), rs.getString("name")));
                    titulNeed.put(rs.getInt("titul_id"), rs.getInt("titul_id"));
                }
            }

            rs.close();
            stat.close();
        } catch ( SQLException e ) {
            e.printStackTrace();
        }
    }

    private void getSmsList () {
        sms = new ArrayList<> ();
        objectNeed = new HashMap<>();

        try {
            stat = connSql.createStatement();
            ResultSet rs = stat.executeQuery("SELECT * FROM ks_Smeta WHERE smetIsSSR = 0");
            while (rs.next()) {
                if (smetNeed.get(rs.getInt("smet_id")) != null) {
                    if (listOpenSmet.get(rs.getInt("smet_id")) != null) {
                    } else {
                        sms.add(new SmetNode(rs.getInt("object_id"), rs.getInt("smet_id"), "Смета " + rs.getString("smetNumber") + " - " + rs.getString("smetName")));
                        objectNeed.put(rs.getInt("object_id"), rs.getInt("object_id"));
                    }
                }
            }

            rs.close();
            stat.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void getSmSectList () {
        smetNeed = new HashMap<>();

        try {
            stat = connSql.createStatement();
            ResultSet rs = stat.executeQuery("SELECT sect_id, smet_id FROM ks_Sm_section");
            while (rs.next()) {
                if (sectNeed.get(rs.getInt(1)) != null) {
                    smetNeed.put(rs.getInt(2), rs.getInt(2));
                }
            }

            rs.close();
            stat.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void getSmrList () {
        sectNeed = new HashMap<>();

        try {
            stat = connSql.createStatement();
            ResultSet rs = stat.executeQuery("SELECT smr_id, sect_id FROM ks_Sm_rows");
            while (rs.next()) {
                if (hashSmetSearch.get(rs.getInt(1)) != null) {
                    sectNeed.put(rs.getInt(2), rs.getInt(2));
                }
            }

            rs.close();
            stat.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void addHierarchy () {
        for (SmetNode curSmc : smc) {
            root.add(curSmc);
            addSmtChildren(curSmc);
        }
    }

    private void addSmtChildren (SmetNode parent) {
        for (SmetNode node : smt) {
            if (node.getParentId() != parent.getId()) continue;
            parent.add(node);
            addSmoChildren(node);
        }
    }

    private void addSmoChildren (SmetNode parent) {
        for (SmetNode node : smo) {
            if (node.getParentId() != parent.getId()) continue;
            parent.add(node);
            addSmsChildren(node);
        }
    }

    private void addSmsChildren (SmetNode parent) {
        for (SmetNode node : sms) {
            if (node.getParentId() != parent.getId()) continue;
            node.isString = true;
            node.setAllowChildren(false);
            parent.add(node);
        }
    }
}
